package com.tydic.enterprise.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tydic.common.core.domain.model.LoginUser;
import com.tydic.common.core.enums.AuditStatusEnum;
import com.tydic.common.core.enums.HisStatusEnum;
import com.tydic.common.core.enums.MessageCategoryEnum;
import com.tydic.common.satoken.utils.LoginHelper;
import com.tydic.enterprise.domain.Enterprise;
import com.tydic.enterprise.domain.EnterpriseChangeHistory;
import com.tydic.enterprise.mapper.EnterpriseChangeHistoryMapper;
import com.tydic.enterprise.mapper.EnterpriseMapper;
import com.tydic.enterprise.service.IEnterpriseService;
import com.tydic.system.service.ISysMessageService;
import com.tydic.workflow.service.IEnterpriseRegisterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Slf4j
@Service
public class EnterpriseServiceImpl implements IEnterpriseService {
    private final EnterpriseMapper enterpriseMapper;

    private final ISysMessageService messageService;

    private final EnterpriseChangeHistoryMapper enterpriseChangeHistoryMapper;
    private final IEnterpriseRegisterService enterpriseRegisterService;

    @Override
    public Enterprise getEnterByUserCode(String userCode) {
        return getEnterpriseByUserCode(userCode);
    }

    @Override
    public Enterprise getEnterById(String id) {
        return enterpriseMapper.selectById(id);
    }

    @Override
    public Enterprise getEntityByLoginUser() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return getEnterpriseByUserCode(loginUser.getUserCode());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEnterpriseInfo(Enterprise enterprise) {
        EnterpriseChangeHistory history = BeanUtil.toBean(enterprise, EnterpriseChangeHistory.class);
        // 是否需要重新启动流程
        if (CharSequenceUtil.isEmpty(enterprise.getProcInstId())) {
            // 调用审核流程
            ProcessInstance processInstance = enterpriseRegisterService.startProcess(enterprise.getId());
            enterprise.setProcInstId(processInstance.getId());
            enterprise.setAuditStatus(AuditStatusEnum.READY.getStatus());
            // 历史记录
            history.setHisStatus(HisStatusEnum.PRE_CHANGE.getStatus());
            history.setAuditStatus(AuditStatusEnum.READY.getStatus());
            history.setProcInstId(processInstance.getId());
            messageService.insertNeedHandler(MessageCategoryEnum.ENTERPRISE, history.getId(), "企业信息待审核", "企业:" + enterprise.getEnterpriseName());
        }
        enterpriseMapper.insertOrUpdate(enterprise);
        enterpriseChangeHistoryMapper.insertOrUpdate(history);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void approveEnterprise(Enterprise enterprise, String comment, boolean pass) {
        enterprise.setAuditStatus(getAuthStatus(pass));
        enterpriseMapper.updateById(enterprise);
        messageService.handleMessage(MessageCategoryEnum.ENTERPRISE, enterprise.getId());
        if (!pass) {
            messageService.insertMessageHandler(MessageCategoryEnum.ENTERPRISE, enterprise.getId(), "企业信息待修改", "企业:" + enterprise.getEnterpriseName(), enterprise.getUserCode());
        }
    }

    private String getAuthStatus(boolean status) {
        return status ? AuditStatusEnum.SUCCESS.getStatus() : AuditStatusEnum.FAIL.getStatus();
    }

    @Override
    public Enterprise getEnterpriseByUserCode(String userCode) {
        return enterpriseMapper.selectOne(Wrappers.lambdaQuery(Enterprise.class).eq(Enterprise::getUserCode, userCode), false);
    }

    @Override
    public Enterprise getByEnterpriseCode(String enterpriseCode) {
        return enterpriseMapper.selectOne(Wrappers.lambdaQuery(Enterprise.class).eq(Enterprise::getEnterpriseCode, enterpriseCode), false);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Enterprise enterprise) {
        enterpriseMapper.updateById(enterprise);
    }
}
